var resourceUrl = getApp().request("imgUrl");
var requestUrl = getApp().request("url");
var header = getApp().request("header");
var app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    process:"",
    shareProcess:"",
    fightCarTypeId:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //每日分享获得的进度
    
    
    //查询是否有车
    if (app.fightCarTypeId==0){
      this.setData({
        fightCarTypeId: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '',
      mask: true
    })
    //查询每日签到状态
    var shareProcess =app.shareLeftN;
    this.setData({
      shareProcess: 3 - shareProcess
    });
    wx.request({
      url: requestUrl + 'findSignInfo',
      method: "POST",
      data: { userId: app.session_key.userId, salt: app.session_key.salt },
      header: header,
      success: (res) => {
        console.log(res)
        if (res.statusCode != 200) {
          getApp().showModal("服务器异常");
          return
        }
        if (res.data.code == 200) {
          this.setData({
            process: res.data.data.today
          });
        } else if (res.data.code == 401 || res.data.code == 301 || res.data.code == 302) {
          console.log(res.data.code)
          getApp().showModal("系统信息有误，请重试！");
        } else if (res.data.code == 800) {
          getApp().showModal(res.data.data.msg);
          wx.hideLoading();
        }
        wx.hideLoading()
      },
      fail:(fai)=>{
        console.log("查询每日签到fail")
        console.log(fai)
        getApp().showModal("网络超时，请检查您的网络");
      }
    });
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    //是否选车
    var fightCarTypeId = app.fightCarTypeId;
    //随机一句宣言
    var declaration = getApp().Refit();
    var myRefitTxt = Math.floor(Math.random() * declaration.length);
    if (fightCarTypeId == 0) {
      return {
        title: getApp().shareTitle1(),
        path: "/pages/index/index"
      }
    } else {
      return {
        title: getApp().shareTitle2(),
        path: "/pages/openShare/openShare?shareId=" + app.session_key.userId + "&carTypeId=" + fightCarTypeId + "&shareRefitNum=" + myRefitTxt + "&carName=SUV概念车",
        success: (re) => {
          if (Object.keys(re).length == 1) {
            return
          }
          var ticket = re.shareTickets[0];
          wx.getShareInfo({
            shareTicket: ticket,
            success: function (Re) {
              console.log(Re)
              var encryptedData = Re.encryptedData;
              var iv = Re.iv;
              //请求分享接口加次数
              wx.request({
                url: requestUrl + 'shareGroupAddDrawNum',
                method: "POST",
                data: { userId: app.session_key.userId, salt: app.session_key.salt, encryptedData: encryptedData, iv: iv },
                header: header,
                success: function (Res) {
                  console.log(Res);
                  if (Res.data.code == 200) {
                    //当日剩余抽取次数减去一
                    var shareLeftN = app.shareLeftN;
                    shareLeftN--;
                    app.shareLeftN=shareLeftN;
                    //剩余抽取次数加3次
                    var drawLeftN = app.drawLeftN;
                    drawLeftN += 3;
                    app.drawLeftN=drawLeftN;
                    //页面分享进度减一
                    var shareProgress = that.data.shareProcess;
                    shareProgress++;
                    that.setData({
                      shareProcess: shareProgress
                    })
                  } else if (Res.data.code == 401 || Res.data.code == 301 || Res.data.code == 302) {
                    getApp().showModal("提交信息有误!")
                  } else if (Res.data.code == 800) {
                    getApp().showModal(Res.data.data.msg);
                    wx.hideLoading();
                  }
                }
              })
            }
          })
        }
      }
    } 
  },
  btnGoShare(){
    if (this.data.fightCarTypeId){
       return
    }else{
      wx.navigateTo({
        url: '../share/share',
      })
    }
  }
})